<?php

class voteComponents extends sfComponents
{
    public function connstr()
    {
        $server = "172.16.0.58";
        //$server="192.168.1.230";
        // $server=".";
        $username = 'sa';
        $password = '123456';
        $database = 'iEasy_new';
        $connstr = "Driver={SQL Server Native Client 10.0};Server=localhost;Database=$database";
        $dbconn = odbc_connect($connstr, $username, $password, SQL_CUR_USE_ODBC);
        if (!$dbconn) {
            die('Could not connect: ');
        }
        else return $dbconn;
    }

    public function executeHotlist(sfWebRequest $request)
    {

        $connection = Doctrine_Manager::connection();
        if ($this->catalogId == null) {
            $sql_recom_pic = "SELECT Celebrity.celebrity_id as celebrity_id,count_i,cast(snapshot_big as varchar(100))snapshot_big,cast(name as varchar(100))name,sex from Celebrity, Vote where vote.celebrity_id=Celebrity.celebrity_id  order by count_i desc";

        }
        else {
            $sql_recom_pic = "SELECT Celebrity.celebrity_id as celebrity_id,count_i,cast(snapshot_big as varchar(100))snapshot_big,cast(name as varchar(100))name,sex from Celebrity, Vote where vote.celebrity_id=Celebrity.celebrity_id and Celebrity.catalog_id=" . $this->catalogId . "  order by count_i desc";
        }
        $STH = $connection->execute($sql_recom_pic);
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        $this->arrMale = array();
        $this->arrFemale = array();
        $this->arrGroup = array();
        $this->recordCount = 3;
        $this->m = 0;
        $this->f = 0;
        $this->g = 0;
        while ($row = $STH->fetch()) {
            // $arr2[]=$row['recommend_content_id'];
            if ('male' == trim($row["sex"]) && $this->m < $this->recordCount) {

                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrMale[] = $currentNews;
                $this->m++;
            }
            if ('female' == trim($row["sex"]) && $this->f < $this->recordCount) {
                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrFemale[] = $currentNews;
                $this->f++;
            }
            if ('group' == trim($row["sex"]) && $this->g < $this->recordCount) {
                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrGroup[] = $currentNews;
                $this->g++;
            }
        }

    }

    public function executeHot(sfWebRequest $request)
    {
        $connection = Doctrine_Manager::connection();
        $sql_daily = "SELECT Celebrity.celebrity_id as celebrity_id,cast(snapshot_big as varchar(100))snapshot_big,cast(name as varchar(100))name,sex,Celebrity.catalog_id as catalog_id from Celebrity, Vote where vote.celebrity_id=Celebrity.celebrity_id and sex='" . $this->sex . "'order by daily_count";
        $sql_weekly = "SELECT Celebrity.celebrity_id as celebrity_id,cast(snapshot_big as varchar(100))snapshot_big,cast(name as varchar(100))name,sex,Celebrity.catalog_id as catalog_id from Celebrity, Vote where vote.celebrity_id=Celebrity.celebrity_id and sex='" . $this->sex . "'order by weekly_count";
        $this->arrChinese = array();
        $this->arrKpop = array();
        $this->arrJpop = array();
        $this->arrWestern = array();
        $this->arrChinese_w = array();
        $this->arrKpop_w = array();
        $this->arrJpop_w = array();
        $this->arrWestern_w = array();
        if ($this->recordCount == null) {
            $this->recordCount = 8;
        }
        if ($this->sex == null) {
            $this->sex = 'male';
        }
        $Daily = $connection->execute($sql_daily);
        $Daily->setFetchMode(PDO::FETCH_ASSOC);
        while ($row = $Daily->fetch()) {
            // $arr2[]=$row['recommend_content_id'];
            if ($row["catalog_id"] == 1000) {
                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrChinese[] = $currentNews;
            }
            if ($row["catalog_id"] == 1001) {
                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrKpop[] = $currentNews;
            }
            if ($row["catalog_id"] == 1002) {
                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrJpop[] = $currentNews;
            }
            if ($row["catalog_id"] == 1003) {
                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrWestern[] = $currentNews;
            }
        }

        $Weekly = $connection->execute($sql_weekly);
        $Weekly->setFetchMode(PDO::FETCH_ASSOC);
        while ($row = $Weekly->fetch()) {
            // $arr2[]=$row['recommend_content_id'];
            if ($row["catalog_id"] == 1000) {
                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrChinese_w[] = $currentNews;
            }
            if ($row["catalog_id"] == 1001) {
                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrKpop_w[] = $currentNews;
            }
            if ($row["catalog_id"] == 1002) {
                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrJpop_w[] = $currentNews;
            }
            if ($row["catalog_id"] == 1003) {
                $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"]);
                $this->arrWestern_w[] = $currentNews;
            }
        }

    }


    function cmp($a, $b)
    {
        if ($a['daily_count'] == $b['daily_count']) {
            return 0;
        }
        return ($a['daily_count'] < $b['daily_count']) ? -1 : 1;
    }
}